package com.link.server.ws.handle;

import com.link.common.packet.RequestOperate;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class HandlerManager {
    private Map<RequestOperate, Handler > handers = new HashMap<>();

    public HandlerManager(@Autowired Handler[] handlers) {
        for (Handler handler : handlers) {
            Class<? extends Handler> aClass = handler.getClass();
            HandleComponent handlerComponent = aClass.getAnnotation(HandleComponent.class);
            handers.put(handlerComponent.value(), handler);
        }

    }

    public void invoke(RequestOperate operate, ByteBuf packet, ChannelHandlerContext ctx) throws Exception {
        Handler  handler = handers.get(operate);
        if (handler == null) {
            return;
        }
        handler.invoke( operate,packet, ctx);
    }
}
